<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <!-- <el-form-item label="上级分类" prop="parent_id" v-if="firstInput">
        <el-select v-model="dataForm.parent_id" placeholder="请选择">
          <el-option v-for="item in typelist" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order_index">
        <el-input-number v-model="dataForm.order_index" :min="1"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  treeDataTranslate
} from '@/utils'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      visible: false,
      firstInput: true,
      dataForm: {
        id: '0',
        parent_id: 0,
        name: null,
        order_index: 1
      },
      typelist: [
        {
          name: '顶级',
          id: 0
        }
      ],
      dataRule: {
        name: [{
          required: true,
          message: '分类名称不能为空',
          trigger: 'blur'
        }],
        order_index: [{
          required: true,
          message: '排序不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  mounted () {

  },
  methods: {
    init (id, parent_id) {
      this.dataForm.id = id || 0
      this.visible = true

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (parent_id != 0) {
        this.firstInput = true
      } else {
        this.firstInput = false
      }
      this.getTypelist()
      if (id != 0 && id != undefined) {
        // 查询
        seviceApi.getServiceGroup(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
          }
        })
      }
    },
    // 获取一级分类
    getTypelist () {
      seviceApi.getFirstServiceGroup().then(res => {
        if (res && res.data.code == 200) {
          if (res.data.list.length > 0) {
            let arrs = [
              {
                name: '顶级',
                id: 0
              }
            ]
            this.typelist = arrs.concat(res.data.list)
          }
        }
      })
    },
    // 提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          var param = {
            parent_id: this.dataForm.parent_id,
            name: this.dataForm.name,
            order_index: this.dataForm.order_index
          }
          if (this.dataForm.id != 0) {
            var key = 'id'
            var id = this.dataForm.id
            param[key] = id
            seviceApi.updateServiceGroup(param).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          } else {
            seviceApi.addServiceGroup(param).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('updataList', 1)
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
